Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmINV540
		Inherits System.Windows.Forms.Form

		Private Sub cboTCITN2_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTCITN2.ClickEvent
			If eventArgs.area = 2 Then
				txtTCITN2.Text = cboTCITN2.BoundText
				txtIMITD2.Text = cboTCITN2.CtlText
				cboTCITN2.Visible = False
			End If

		End Sub
		Sub CreateICT(ByRef TagNo As TextBox, ByRef TCITN2 As TextBox, ByRef TCITN3 As TextBox, ByRef TCALL As CheckBox)
			Dim SQL As String
			Dim TCVAL As String
			Dim TCOHQ As String
			Dim TCOHW As String
			Dim TCLOT As String
			Dim TCWHC As String
			Dim TCITN As String
			Dim TCDTE As String
			Dim TCTNO As String
			Dim IMITD, WMDSC As Object
			Dim RecILI, RecIIM, RecIWI As ADODB.Recordset
			If TCALL.Checked Then
				RecIIM = DBconn.Execute("Select * FROM IIM ORDER BY IMTYP,IMCLC,IMITN")
			Else
				RecIIM = DBconn.Execute("Select * from IIM WHERE IMITN>='" & TCITN2.Text & "' AND IMITN<='" & TCITN3.Text & "' ORDER BY IMTYP,IMCLC,IMITN")
			End If
			TCTNO = TagNo.Text

			TCDTE = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")
			Do While Not RecIIM.EOF
				TCITN = RecIIM.Fields("imitn").Value
				TCWHC = ""
				TCLOT = ""
				TCOHW = RecIIM.Fields("IMOBW").Value + RecIIM.Fields("IMRCW").Value + RecIIM.Fields("IMAJW").Value - RecIIM.Fields("IMISW").Value
				TCOHQ = RecIIM.Fields("IMOBU").Value + RecIIM.Fields("IMRCU").Value + RecIIM.Fields("IMAJU").Value - RecIIM.Fields("IMISU").Value
				IMITD = RecIIM.Fields("IMITD").Value
				If RecIIM.Fields("IMPER").Value = "Unt" Then
					TCVAL = TCOHQ * RecIIM.Fields("IMATC").Value
				Else
					TCVAL = TCOHW * RecIIM.Fields("IMATC").Value
				End If
				SQL = "INSERT INTO ICTTEMP(TCTNO,TCITN2,TCITN3,TCITN,TCWHC,TCLOT" & ",TCOHW,TCOHQ,TCPCW,TCPCQ,TCDTE,TCVAL,TCALL,SOMAY,IMITD) VALUES " & "('" & TCTNO & "','" & (TCITN2.Text) & "','" & (TCITN3.Text) & "','" & TCITN & "','" & TCWHC & "','" & TCLOT & "'" & "," & TCOHW & "," & TCOHQ & ",0,0,'" & TCDTE & "'," & TCVAL & "," & TCALL.Checked & "," & MAYSO & ",'" & IMITD & "')"

				DBconn.Execute(SQL)

				RecIWI = DBconn.Execute("SELECT * FROM IWI WHERE IWITN='" & TCITN & "' ORDER BY IWWHC")
				Do While Not RecIWI.EOF
					TCWHC = RecIWI.Fields("IWWHC").Value
					TCOHW = RecIWI.Fields("IWOBW").Value + RecIWI.Fields("IWRCW").Value + RecIWI.Fields("IWAJW").Value - RecIWI.Fields("IWISW").Value
					TCOHQ = RecIWI.Fields("IWOBU").Value + RecIWI.Fields("IWRCU").Value + RecIWI.Fields("IWAJU").Value - RecIWI.Fields("IWISU").Value
					WMDSC = Find("IWM", "WMDSC", " WHERE WMWHC='" & TCWHC & "'")
					If RecIIM.Fields("IMPER").Value = "Unt" Then
						TCVAL = TCOHQ * RecIIM.Fields("IMATC").Value
					Else
						TCVAL = TCOHW * RecIIM.Fields("IMATC").Value
					End If
					SQL = "INSERT INTO ICTTEMP(TCTNO,TCITN2,TCITN3,TCITN,TCWHC,TCLOT" & ",TCOHW,TCOHQ,TCPCW,TCPCQ,TCDTE,TCVAL,TCALL,SOMAY,WMDSC) VALUES " & "('" & TCTNO & "','" & TCITN2.Text & "','" & TCITN3.Text & "','" & TCITN & "','" & TCWHC & "','" & TCLOT & "'" & "," & TCOHW & "," & TCOHQ & ",0,0,'" & TCDTE & "'," & TCVAL & "," & TCALL.Checked & "," & MAYSO & ",'" & WMDSC & "')"

					DBconn.Execute(SQL)

					RecILI = DBconn.Execute("SELECT * FROM ILI WHERE ILITN='" & TCITN & "' AND ILWHC='" & TCWHC & "' ORDER BY ILWHC")
					Do While Not RecILI.EOF
						TCLOT = RecILI.Fields("ILLOT").Value
						TCOHW = RecILI.Fields("ILOBW").Value + RecILI.Fields("ILRCW").Value + RecILI.Fields("ILAJW").Value - RecILI.Fields("ILISW").Value
						TCOHQ = RecILI.Fields("ILOBU").Value + RecILI.Fields("ILRCU").Value + RecILI.Fields("ILAJU").Value - RecILI.Fields("ILISU").Value
						If RecIIM.Fields("IMPER").Value = "Unt" Then
							TCVAL = TCOHQ * RecIIM.Fields("IMATC").Value
						Else
							TCVAL = TCOHW * RecIIM.Fields("IMATC").Value
						End If
						SQL = "INSERT INTO ICTTEMP(TCTNO,TCITN2,TCITN3,TCITN,TCWHC,TCLOT" & ",TCOHW,TCOHQ,TCPCW,TCPCQ,TCDTE,TCVAL,TCALL,SOMAY,IMITD,WMDSC) VALUES " & "('" & TCTNO & "','" & TCITN2.Text & "','" & TCITN3.Text & "','" & TCITN & "','" & TCWHC & "','" & TCLOT & "'" & "," & TCOHW & "," & TCOHQ & ",0,0,'" & TCDTE & "'," & TCVAL & "," & TCALL.Checked & "," & MAYSO & ",'" & IMITD & "','" & WMDSC & "')"

						DBconn.Execute(SQL)

						RecILI.MoveNext()
					Loop
					TCLOT = ""
					RecIWI.MoveNext()
				Loop
				TCWHC = ""
				RecIIM.MoveNext()
			Loop
			Adodc1.RecordSource = "SELECT ICTTEMP.TCITN, IIM.IMITD, IWM.WMdsc,ICTTEMP.TCWHC, ICTTEMP.TCLOT, " & "ICTTEMP.TCOHQ, ICTTEMP.TCOHW, ICTTEMP.TCPCQ, ICTTEMP.TCPCW, ICTTEMP.TCDTC," & "ICTTEMP.TCVAL, ICTTEMP.TCREM FROM ICTTEMP INNER JOIN " & " IIM ON ICTTEMP.TCITN = IIM.IMITN LEFT OUTER JOIN " & " IWM ON ICTTEMP.TCWHC = IWM.WMWHC WHERE ICTTEMP.SOMAY= " & MAYSO & "  order by TCIDN "
			Adodc1.Refresh()
		End Sub
		Sub tam()

		End Sub

		Private Sub cboTCITN3_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTCITN3.ClickEvent
			If eventArgs.area = 2 Then
				txtTCITN3.Text = cboTCITN3.BoundText
				txtIMITD3.Text = cboTCITN3.CtlText
				If ISADDING Then CreateICT(txtTCTNo, txtTCITN2, txtTCITN3, chkTCALL)
				cboTCITN3.Visible = False
			End If
		End Sub

		Private Sub chkTCALL_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTCALL.CheckStateChanged
			If chkTCALL.CheckState = 1 Then
				CreateICT(txtTCTNo, txtTCITN2, txtTCITN3, chkTCALL)
			End If
		End Sub

		Private Sub cmdAddNew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddNew.Click
			Dim Response As Object
			Dim TXTTCNO As Object
			ISADDING = True
			ISEDITING = True
			txtTCTNo.Text = "PC" & VB.Right(Str(Year(Today)), 2) & InsertPreCharacter("0", Month(Today), 2) & InsertPreCharacter("0", VB.Day(Today), 2)
			Dim RECICT As ADODB.Recordset
			RECICT = DBconn.Execute("SELECT * FROM ICT WHERE TCTNO='" & TXTTCNO & "'")
			If Not RECICT.EOF Then
				Response = MsgBox("This count tag is already exist. Do you want to overwrite it?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.DefaultButton2, "Chu y'")
				If Response = MsgBoxResult.No Then
					Exit Sub
				End If
			End If
			ReadToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			'Dim myform As New frmReview
			'myform.Tag = "INV540"
			' myform.Show()
			' txtTCTNo.Text = frmReview.GridList.Columns(1).Text
			frmReview.Tag = "INV540"
			frmReview.ShowDialog()
			If frmReview.Tag <> "" Then
				txtTCTNo.Text = frmReview.GridList.Columns(1).Text
				frmReview.Close()
			End If

			ReadToScreen()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			WriteToICT()
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
		End Sub

		Sub OpenFile()
			Dim DelStatus As Object
			Dim FieldDel As Object
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "SELECT ICTTEMP.TCITN, IIM.IMITD, IWM.WMdsc,ICTTEMP.TCWHC, ICTTEMP.TCLOT, " & "ICTTEMP.TCOHQ, ICTTEMP.TCOHW, ICTTEMP.TCPCQ, ICTTEMP.TCPCW, ICTTEMP.TCDTC," & "ICTTEMP.TCVAL, ICTTEMP.TCREM FROM ICTTEMP INNER JOIN " & " IIM ON ICTTEMP.TCITN = IIM.IMITN LEFT OUTER JOIN " & " IWM ON ICTTEMP.TCWHC = IWM.WMWHC WHERE ICTTEMP.SOMAY= " & MAYSO & "  order by TCIDN "
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			FieldDel = "IMRID"
			DelStatus = "IZ"
			Adodc2.RecordSource = "select IMITN, IMITN + ' ' + IMITD as IMITD from IIM where " & FieldDel & " <>'" & DelStatus & "'"
			Adodc2.Refresh()
		End Sub
		Sub ReadToScreen()
			Dim SQL As String
			DBconn.Execute("Delete  from ICTTEMP where SOMAY=" & MAYSO)
			Dim RECICT As ADODB.Recordset
			If ISADDING Then
				txtTCDTE.Text = CStr(Now)
				txtTCITN2.Text = ""
				txtIMITD2.Text = ""
				txtTCITN3.Text = ""
				txtIMITD3.Text = ""
				cboTCITN2.Visible = False
				cboTCITN3.Visible = False
				chkTCALL.CheckState = System.Windows.Forms.CheckState.Unchecked
			Else
				SQL = "INSERT INTO ICTTEMP (TCTNO,TCITN,TCITN2,TCITN3, TCWHC, TCLOT " & ",TCOHQ, TCOHW, TCPCQ, TCPCW, TCDTC,TCDTE,TCALL" & ",TCVAL , TCREM,SOMAY,IMITD, WMDSC)" & " select ICT.TCTNO,ICT.TCITN,ICT.TCITN2,ICT.TCITN3,ICT.TCWHC,ICT.TCLOT " & ",ICT.TCOHQ, ICT.TCOHW, ICT.TCPCQ, ICT.TCPCW, ICT.TCDTC,ICT.TCDTE,ICT.TCALL" & ",TCVAL , TCREM," & MAYSO & ",IIM.IMITD,IWM.WMDSC FROM ICT INNER JOIN " & " IIM ON ICT.TCITN = IIM.IMITN LEFT OUTER JOIN " & " IWM ON ICT.TCWHC = IWM.WMWHC " & " WHERE TCTNO='" & txtTCTNo.Text & "'"
				DBconn.Execute(SQL)
				RECICT = DBconn.Execute("Select * from ICTTEMP where SOMAY=" & MAYSO)
				If RECICT.EOF = False Then
					With RECICT
						txtTCDTE.Text = .Fields("TCDTE").Value
						txtTCITN2.Text = .Fields("TCITN2").Value
						txtIMITD2.Text = Find("IIM", "IMITD", "WHERE IMITN='" & txtTCITN2.Text & "'")
						txtTCITN3.Text = .Fields("TCITN3").Value
						txtIMITD3.Text = Find("IIM", "IMITD", "WHERE IMITN='" & txtTCITN3.Text & "'")
						cboTCITN2.Visible = False
						cboTCITN3.Visible = False
						chkTCALL.CheckState = .Fields("TCALL").Value
					End With
				End If
			End If
			'Adodc1.RecordSource = "SELECT ICTTEMP.TCITN, IIM.IMITD, IWM.WMdsc,ICTTEMP.TCWHC, ICTTEMP.TCLOT, " & _
			'"ICTTEMP.TCOHQ, ICTTEMP.TCOHW, ICTTEMP.TCPCQ, ICTTEMP.TCPCW, ICTTEMP.TCDTC," & _
			'"ICTTEMP.TCVAL, ICTTEMP.TCREM FROM ICTTEMP INNER JOIN " & _
			'" IIM ON ICTTEMP.TCITN = IIM.IMITN LEFT OUTER JOIN " & _
			'" IWM ON ICTTEMP.TCWHC = IWM.WMWHC WHERE ICTTEMP.SOMAY= " & MAYSO & "  order by TCIDN "
			Adodc1.RecordSource = "SELECT ICTTEMP.TCITN, ICTTEMP.IMITD, ICTTEMP.WMdsc,ICTTEMP.TCWHC, ICTTEMP.TCLOT, " & "ICTTEMP.TCOHQ, ICTTEMP.TCOHW, ICTTEMP.TCPCQ, ICTTEMP.TCPCW, ICTTEMP.TCDTC," & "ICTTEMP.TCVAL, ICTTEMP.TCREM FROM ICTTEMP  WHERE ICTTEMP.SOMAY= " & MAYSO & "  order by TCIDN "
			Adodc1.Refresh()

		End Sub

		Private Sub frmINV540_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			OpenFile()
			VB6_AddADODataBinding()
			Me.cboTCITN2.RowSource = ADOBind_Adodc2.DataSource
			Me.cboTCITN3.RowSource = ADOBind_Adodc2.DataSource
			Dim myDataCaptions As String() = {"Item code", "Item Desc", "Warehouse", "LOT", "Onhand Unt", "Onhand Wgt", "Physical Unt", "Physical Wgt", "Count Date", "Value", "Remark"}
			Dim myDataFields As String() = {"TCITN", "IMITD", "WMDSC", "TCLOT", "TCOHQ", "TCOHW", "TCPCQ", "TCPCW", "TCDTC", "TCVAL", "TCREM"}
			setDataGridView(myDataCaptions, myDataFields, Me.grdICT)

		End Sub

		Private Sub txtTCITN2_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTCITN2.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtTCITN2.Text & "'")
				If Result = "" Then
					cboTCITN2.Visible = True
					cboTCITN2.Focus()
				Else
					txtIMITD2.Text = Result
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTCITN3_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTCITN3.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtTCITN3.Text & "'")
				If Result = "" Then
					cboTCITN3.Visible = True
					cboTCITN3.Focus()
				Else
					txtIMITD3.Text = Result
					If ISADDING Then CreateICT(txtTCTNo, txtTCITN2, txtTCITN3, chkTCALL)
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub WriteToICT()
			Dim SQL As Object
			DBconn.Execute("DELETE FROM ICT WHERE TCTNO='" & txtTCTNo.Text & "'")
			SQL = "INSERT INTO ICT(TCTNO,TCITN,TCITN2,TCITN3,TCWHC, TCLOT " & ",TCOHQ, TCOHW, TCPCQ, TCPCW, TCDTC,TCDTE,TCALL" & ",TCVAL , TCREM,SOMAY)" & " select TCTNO,TCITN,TCITN2,TCITN3,TCWHC, TCLOT " & ",TCOHQ, TCOHW, TCPCQ, TCPCW, TCDTC,TCDTE,TCALL" & ",TCVAL , TCREM," & MAYSO & " FROM ICTTEMP WHERE SOMAY=" & MAYSO & "ORDER BY TCIDN"
			DBconn.Execute(SQL)
		End Sub

		Sub ButtonStatus()
			cmdAddNew.Enabled = Not ISEDITING
			cmdReview.Enabled = Not ISEDITING

			cmdSave.Enabled = ISEDITING
			cmdCancel.Enabled = ISEDITING
			FrameCountTag.Enabled = ISADDING
		End Sub
	End Class
End Namespace